package net.zigin.nhi.could.waste.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import net.zigin.commons.util.AssertUtil;
import net.zigin.commons.util.ReflectUtil;
import net.zigin.nhi.could.waste.annotation.LoginWhitePathAnnotation;
import net.zigin.nhi.could.waste.dto.SysMenuDto;
import net.zigin.nhi.could.waste.exception.EpiCloudException;
import net.zigin.nhi.could.waste.model.SysMenu;
import net.zigin.nhi.could.waste.service.SysMenuService;

import net.zigin.nhi.could.waste.vo.JsonForResult;
import net.zigin.nhi.could.waste.vo.SysMenuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * SysMenu控制器类
 * @ClassName: SysMenuController
 * @author mgw1986
 * @date 2021-9-7 19:08:53
 * @version V1.0
 */
@Api(tags = "菜单管理")
@RestController
@RequestMapping("/sysMenu")
public class SysMenuController extends BaseController {
	@Autowired
	private SysMenuService sysMenuService;

	/**
	 * 查询菜单详情
	 * @param id 菜单id
	 * @return 菜单详情
	 */
	@GetMapping("/getById/{id}")
	@ApiOperation("查询菜单详情")
	@ApiImplicitParam(name = "id", value = "菜单id", required = true, dataType = "String")
	public JsonForResult<SysMenuVo> getById(@PathVariable String id) {
		AssertUtil.isNull(id, "菜单id不能为空");
		SysMenu menu = sysMenuService.selectByPrimaryKey(id);
		SysMenuVo appVo = ReflectUtil.copyProperties(menu, SysMenuVo.class);
		return JsonForResult.<SysMenuVo>builder().data(appVo).success(true).build();
	}

	/**
	 * 保存或编辑菜单
	 * @param menuDto 菜单详情
	 * @return 菜单IDMenuDto
	 */
	@PostMapping("/save")
	@ApiOperation("保存或编辑菜单")
	@ApiImplicitParam(name = "menuDto", value = "菜单信息", required = true, dataType = "SysMenuDto")
	public JsonForResult save(@RequestBody SysMenuDto menuDto) {
		String id = sysMenuService.saveOrUpdate(menuDto);
		return JsonForResult.builder().data(id).success(true).build();
	}

	/**
	 * 菜单列表
	 * @param menuDto 菜单查询条件
	 * @return 菜单列表
	 */
	@PostMapping("/getList")
	@ApiOperation("菜单列表")
	@ApiImplicitParam(name = "menuDto", value = "菜单信息", required = true, dataType = "SysMenuDto")
	public JsonForResult<List<SysMenuVo>> getList(@RequestBody SysMenuDto menuDto) {
		List<SysMenuVo> menuList = sysMenuService.getMenuList(menuDto);
		Integer count = sysMenuService.getMenuCount(menuDto);
		return JsonForResult.<List<SysMenuVo>>builder()
				.data(menuList).total(count)
				.success(true).build();
	}

	/**
	 * 获得角色关联的菜单列表信息
	 * @param roleId 角色ID
	 * @return 菜单列表
	 */
	@LoginWhitePathAnnotation
	@GetMapping("/getListByRoleId/{roleId}")
	@ApiOperation("获得角色关联的菜单列表信息")
	@ApiImplicitParam(name = "roleId", value = "角色id", required = true, dataType = "String")
	public JsonForResult<List<SysMenuVo>> getListByRoleId(@PathVariable String roleId) {
		List<SysMenuVo> menuVos = sysMenuService.infoByRoleId(roleId);
		return JsonForResult.<List<SysMenuVo>>builder().data(menuVos).success(true).build();
	}

	/**
	 * 删除菜单
	 * @param id 菜单ID
	 * @return
	 */
	@DeleteMapping("/remove/{id}")
	@ApiOperation("删除菜单")
	@ApiImplicitParam(name = "id", value = "菜单id", required = true, dataType = "String")
	public JsonForResult remove(@PathVariable String id) {
		// todo 添加判断
		AssertUtil.isNull(id, "菜单id不能为空");
		if(sysMenuService.judgeRemove(id)){
			return JsonForResult.builder().success(false).message("该菜单下已绑定角色或存在下级菜单，不能删除！").build();
		}
		sysMenuService.deleteByPrimaryKey(id);
		return JsonForResult.builder().success(true).build();
	}


}
